package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.TemplateFieldAddDTO;
import org.jsola.hr.dto.TemplateFieldUpdateDTO;
import org.jsola.hr.query.TemplateFieldQuery;
import org.jsola.hr.service.ITemplateFieldService;
import org.jsola.hr.vo.TemplateFieldListVO;
import org.jsola.hr.vo.TemplateFieldVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * @author zhr
 */
@RestController("hrTemplateFieldController")
@RequestMapping(HrConstants.API_PATH + "/v1/templateFields")
public class TemplateFieldController implements ITemplateFieldControllerAPI {

    @Autowired
    private ITemplateFieldService templateFieldService;

    @Override
    @PostMapping
    public Result<TemplateFieldVO> save(@Valid @RequestBody TemplateFieldAddDTO templateFieldAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增模板字段权限校验

        return Result.success(
                templateFieldService.save(templateFieldAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TemplateFieldUpdateDTO templateFieldUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改模板字段权限校验

        int count = templateFieldService.update(templateFieldUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{templateFieldId}")
    public Result<Integer> delete(@PathVariable String templateFieldId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除模板字段权限校验

        int count = templateFieldService.deleteByIds(tokenUser, templateFieldId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] templateFieldIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除模板字段权限校验

        int count = templateFieldService.deleteByIds(tokenUser, templateFieldIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{templateFieldId}")
    public Result<TemplateFieldVO> findById(@PathVariable String templateFieldId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板字段权限校验

        return Result.success(
            templateFieldService.selectById(templateFieldId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TemplateFieldListVO>> listAll(@Valid TemplateFieldQuery templateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板字段权限校验

        return Result.success(
            templateFieldService.select(templateFieldQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TemplateFieldListVO>> page(@Valid TemplateFieldQuery templateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            templateFieldService.selectPage(templateFieldQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/fieldListAll")
    public Result<Map> fieldListAll(@Valid TemplateFieldQuery templateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板字段权限校验

        return Result.success(
//                templateFieldService.selectFieldListAll(templateFieldQuery, tokenUser.getSiteId())
                HrConstants.TEMPLATE_FIELD_MAP
        );
    }
}

